<template>
	<a-card :bordered="false">
		<div style="color: red; text-align: center;font-size:20px;">
			用户ID：(1598286460538232834 王磊)、(1593792956206669825 胡威)、(1593793037110599681 汤宝龙)
		</div>
		<a-tabs v-model:activeKey="activeKey" tab-position="left">
			<a-tab-pane :key="0" tab="1.开门信息数据">
				<a-form :model="formOpenDoor">
					<a-form-item label="柜子：" name="cabinetInfoId">
						<a-select
							v-model:value="formOpenDoor.cabinetInfoId"
							placeholder="请选择柜子"
							:options="cabinetInfoOptions"
							allow-clear
							@change="loadCabinetDoorList"
						/>
					</a-form-item>
					<a-form-item label="柜门：" name="cabinetDoorId">
						<a-select
							v-model:value="formOpenDoor.cabinetDoorId"
							placeholder="请选择柜门"
							:options="cabinetDoorOptions"
							allow-clear
						/>
					</a-form-item>
					<a-form-item label="rfid：" name="rfids">
						<a-input v-model:value.trim="formOpenDoor.rfids" placeholder="rfid，多个以逗号隔开"/>
					</a-form-item>
					<a-form-item label="用户id：" name="openerCards">
						<a-input v-model:value.trim="formOpenDoor.openerCards" placeholder="用户id，多个以逗号隔开" />
					</a-form-item>
				</a-form>
				<a-button type="primary" @click="onSubmitOpenDoor">开门</a-button>
			</a-tab-pane>
			<a-tab-pane :key="1" tab="2.关门信息数据">
				<a-form :model="formCloseDoor">
					<a-form-item label="柜子：" name="cabinetInfoId">
						<a-select
							v-model:value="formCloseDoor.cabinetInfoId"
							placeholder="请选择柜子"
							:options="cabinetInfoOptions"
							allow-clear
							@change="loadCabinetDoorList"
						/>
					</a-form-item>
					<a-form-item label="柜门：" name="cabinetDoorId">
						<a-select
							v-model:value="formCloseDoor.cabinetDoorId"
							placeholder="请选择柜门"
							:options="cabinetDoorOptions"
							allow-clear
						/>
					</a-form-item>
					<a-form-item label="rfid：" name="rfids">
						<a-input v-model:value.trim="formCloseDoor.rfids" placeholder="rfid，多个以逗号隔开"/>
					</a-form-item>
				</a-form>
				<a-button type="primary" @click="onSubmitCloseDoor">关门</a-button>
			</a-tab-pane>
			<a-tab-pane :key="2" tab="3.盘库信息">
				<a-form :model="formInventory">
					<a-form-item label="柜子：" name="cabinetInfoId">
						<a-select
							v-model:value="formInventory.cabinetInfoId"
							placeholder="请选择柜子"
							:options="cabinetInfoOptions"
							allow-clear
							@change="loadCabinetDoorList"
						/>
					</a-form-item>
					<a-form-item label="柜门：" name="cabinetDoorId">
						<a-select
							v-model:value="formInventory.cabinetDoorId"
							placeholder="请选择柜门"
							:options="cabinetDoorOptions"
							allow-clear
						/>
					</a-form-item>
					<a-form-item label="rfid：" name="rfids">
						<a-input v-model:value.trim="formInventory.rfids" placeholder="rfid，多个以逗号隔开"/>
					</a-form-item>
					<a-form-item label="卡号：" name="operator">
						<a-input v-model:value.trim="formInventory.operator"/>
					</a-form-item>
				</a-form>
				<a-button type="primary" @click="onSubmitInventory">盘库</a-button>
			</a-tab-pane>
			<!--<a-tab-pane :key="3" tab="4.自动盘库">无</a-tab-pane>
			<a-tab-pane :key="4" tab="5.客户端启动"></a-tab-pane>
			<a-tab-pane :key="6" tab="6.离线上报"></a-tab-pane>
			<a-tab-pane :key="7" tab="7.根据用户卡号获取用户信息"></a-tab-pane>
			<a-tab-pane :key="8" tab="8.根据用户卡号获取柜门操作权限"></a-tab-pane>
			<a-tab-pane :key="9" tab="9.获取指定柜所有柜门权限列表"></a-tab-pane>
			<a-tab-pane :key="10" tab="10.webHook提交事件"></a-tab-pane>-->
		</a-tabs>
	</a-card>
</template>

<script setup name="ApiTool">
	import bizCabinetInfoApi from '@/api/biz/bizCabinetInfoApi'
	import bizCabinetDoorApi from '@/api/biz/bizCabinetDoorApi'
	import bizUserApi from '@/api/biz/bizUserApi'
	// import md5 from 'js-md5'
	import bizDeviceApiTool from '@/api/biz/bizDeviceApiTool'
	import {message} from 'ant-design-vue'

	const activeKey = ref(0)
	const formOpenDoor = ref({})
	const formCloseDoor = ref({})
	const formInventory = ref({})
	const cabinetInfoList = ref([])
	const cabinetDoorList = ref([])
	const cabinetInfoOptions = ref([])
	const cabinetDoorOptions = ref([])

	bizCabinetInfoApi.bizCabinetList().then((res) => {
		cabinetInfoList.value = res
		cabinetInfoOptions.value = res
			.filter((item) => item.signToken !== null)
			.map((item) => {
				return {
					label: item.cabinetName,
					value: item.id
				}
			})
	})

	const loadCabinetDoorList = (cabinetInfoId) => {
		bizCabinetDoorApi
			.bizCabinetDoorList({
				cabinetInfoId: cabinetInfoId
			})
			.then((res) => {
				cabinetDoorList.value = res
				cabinetDoorOptions.value = res.map((item) => {
					return {
						label: item.cabinetDoorName,
						value: item.id
					}
				})
			})
	}

	const onSubmitOpenDoor = () => {
		let cabinetcode = cabinetInfoList.value.filter((item) => item.id === formOpenDoor.value.cabinetInfoId)[0].cabinetCode
		let doorcode = cabinetDoorList.value.filter((item) => item.id === formOpenDoor.value.cabinetDoorId)[0].cabinetDoorCode
		let json = {
			data: {
				cabinetcode: cabinetcode,
				doorcode: doorcode,
				rfids: formOpenDoor.value.rfids.trim().split(','),
				opener: formOpenDoor.value.openerCards.trim().split(',')
			},
			timestamp: new Date().getTime()
		}
		bizDeviceApiTool.bizDeviceApiToolOpenDoor(json).then((res) => {
			if (res.succ) {
				message.success('操作成功')
			} else {
				message.error(res.errmsg)
			}
		})
		// console.log('json=', json)
	}

	const onSubmitCloseDoor = () => {
		let cabinetcode = cabinetInfoList.value.filter((item) => item.id === formCloseDoor.value.cabinetInfoId)[0].cabinetCode
		let doorcode = cabinetDoorList.value.filter((item) => item.id === formCloseDoor.value.cabinetDoorId)[0]
			.cabinetDoorCode
		let json = {
			data: {
				cabinetcode: cabinetcode,
				doorcode: doorcode,
				rfids: formCloseDoor.value.rfids.trim().split(',')
			},
			timestamp: new Date().getTime()
		}
		bizDeviceApiTool.bizDeviceApiToolCloseDoor(json).then((res) => {
			if (res.succ) {
				message.success('操作成功')
			} else {
				message.error(res.errmsg)
			}
		})
	}

	const onSubmitInventory = () => {
		let cabinetcode = cabinetInfoList.value.filter((item) => item.id === formInventory.value.cabinetInfoId)[0].cabinetCode
		let doorcode = cabinetDoorList.value.filter((item) => item.id === formInventory.value.cabinetDoorId)[0].cabinetDoorCode
		let json = {
			data: [
				{
					cabinetcode: cabinetcode,
					doorcode: doorcode,
					rfids: formInventory.value.rfids.trim().split(',')
				}
			],
			timestamp: new Date().getTime(),
			operator: formInventory.value.operator
		}
		bizDeviceApiTool.bizDeviceApiToolInventory(json).then((res) => {
			if (res.succ) {
				message.success('操作成功')
			} else {
				message.error(res.errmsg)
			}
		})
		// console.log('json=', json)
	}
</script>
